package com.hxx.day6;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
 * collect收集
 */
public class TestDemo4 {
    /**
     * 收集到集合
     */
    @Test
    public void test14(){
        //创建集合
        List<Student> list = List.of(
                new Student("张三",18),
                new Student("李四",19),
                new Student("王五",20));
        //收集到集合
        List<Student> list1 = list.stream().collect(Collectors.toList());
        //list集合
        List<String> list2 = list.stream()
                .map(Student::getName)
                .collect(Collectors.toList());
        //set集合
        Set<String> set = list.stream()
                .map(Student::getName)
                .collect(Collectors.toSet());
        //map集合
        Map<String, Integer> map = list.stream()
                .collect(Collectors.toMap(Student::getName, Student::getAge));
        //HashSet
        Set<String> set1 = list.stream()
                .map(Student::getName)
                .collect(Collectors.toCollection(HashSet::new));
    }

    /**
     * 使用收集获取数量
     */
    @Test
    public void test15(){
        //创建集合
        List<Student> list = List.of(
                new Student("张三",18),
                new Student("李四",19),
                new Student("王五",20));
        //获取数量
        long count = list.stream().collect(Collectors.counting());
    }
    /**
     * 平均值
     */
    @Test
    public void test16(){
        //创建集合
        List<Student> list = List.of(
                new Student("张三",18),
                new Student("李四",19),
                new Student("王五",20));
        //获取平均值
        double average = list.stream().collect(Collectors.averagingDouble(Student::getAge));
    }
    /**
     * max min avg sum count
     */
    @Test
    public void test17(){
        //创建集合
        List<Student> list = List.of(
                new Student("张三",18),
                new Student("李四",19),
                new Student("王五",20));
        //获取
        IntSummaryStatistics collect = list.stream()
                .collect(Collectors.summarizingInt(Student::getAge));
        //获取最大值
        int max = collect.getMax();
        //获取最小值
        int min = collect.getMin();
        //获取平均值
        double average = collect.getAverage();
        //获取总和
        long sum = collect.getSum();
        //获取数量
        long count = collect.getCount();
    }
    /**
     * 并行流
     */
    @Test
    public void test18(){
        //消耗的时间(并行流)
        Instant start = Instant.now();
        LongStream.rangeClosed(0,1000000000L).parallel().reduce(0,Long::sum);
        Instant end = Instant.now();
        System.out.println("消耗的时间:"+ Duration.between(end,start).toMillis());
    }
    /**
     * 串行流
     */
    @Test
    public void test19(){
        //消耗的时间(串行流)
        Instant start = Instant.now();
        LongStream.rangeClosed(0,1000000000L).reduce(0,Long::sum);
        Instant end = Instant.now();
        System.out.println("消耗的时间:"+ Duration.between(end,start).toMillis());
    }
}



















